package com.shisfish.library.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;

import com.shisfish.library.Impl.BookImpl;
import com.shisfish.library.Impl.RecommendImpl;
import com.shisfish.library.Model.Recommend;
import com.shisfish.library.Prere.Login;
import com.shisfish.library.Utils.Global.GlobalVariable;
import com.shisfish.library.Utils.Global.toJsonUtils;
import com.shisfish.library.Utils.PageModel.Page;
import com.shisfish.library.Utils.PageModel.PageUtils;

@Path("recommend")
public class RecommendController extends GlobalVariable {

	private RecommendImpl recommendImpl = new RecommendImpl();
	
	@GET
	@Path("selectAllPage/{type}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectAllPage(@PathParam("type") int type, @PathParam("currentPage") int currentPage, 
			@PathParam("pageSize") int pageSize) {
		
		Map<String, Object> globalMap = new HashMap<String, Object>();
			
		int count = recommendImpl.countType(type);
		
		if (count > 0) {
			Page page = PageUtils.createPage(count, currentPage, pageSize);
			
			List<Recommend> recommends = recommendImpl.selectTypePage(type, page.getBeginIndex(), pageSize);
			
			if (recommends.size() > 0) {
				globalMap.put("list", recommends);
				globalMap.put("page", page);
				globalMap.put(STATUS_CODE, SUCCESS);
			}
			
		} 
		else if (count == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectAllPageE/{employee}/{type}/{currentPage}/{pageSize}")
	@Produces("application/json; charset=utf-8")
	public String selectAllPageE(@PathParam("employee") String employee, @PathParam("currentPage") int currentPage, 
			@PathParam("pageSize") int pageSize, @PathParam("type") int type) {
		
		Map<String, Object> globalMap = new HashMap<String, Object>();
			
		int count = recommendImpl.countTypeE(employee, type);
		
		if (count > 0) {
			Page page = PageUtils.createPage(count, currentPage, pageSize);
			
			List<Recommend> recommends = recommendImpl.selectTypePageE(employee, type, page.getBeginIndex(), pageSize);
			
			if (recommends.size() > 0) {
				globalMap.put("list", recommends);
				globalMap.put("page", page);
				globalMap.put(STATUS_CODE, SUCCESS);
			}
			
		} 
		else if (count == 0) {
			globalMap.put(STATUS_CODE, SELECT_EMPTY);
		}
		else {
			globalMap.put(STATUS_CODE, SELECT_WR);
		}
		return toJsonUtils.map2Json(globalMap);
	}
	
	@GET
	@Path("selectByEmployee/{employee}")
	@Produces("application/json; charset=utf-8")
	public String selectByEmployee(@PathParam("employee") String employee) {
		
		Map<String, Object> globalMap = new HashMap<String, Object>();
			
		int count1 = recommendImpl.countTypeE(employee, 0);
		int count2 = recommendImpl.countTypeE(employee, 1);
		int count3 = recommendImpl.countTypeE(employee, 2);
		int count4 = recommendImpl.countTypeE(employee, 3);
		
		globalMap.put("total", count1);
		globalMap.put("not", count2);
		globalMap.put("reject", count3);
		globalMap.put("accept", count4);
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("insertARecommend/{employee}")
	@Produces("application/json; charset=utf-8")
	public String insertARecommend(@PathParam("employee") String employee, @FormParam("title") String title, 
			@FormParam("author") String author, @FormParam("publisher") String publisher, 
			@FormParam("pubdate") String pubdate, 
			@FormParam("isbn") String isbn, @FormParam("message") String message, 
			@Context HttpServletRequest request) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginUser(employee, request)) {
			isbn = isbn.replace("-", "");
			int count = new BookImpl().countByISBN(isbn);
			if (count == 0) {
				Recommend recommend = new Recommend(title, author, publisher, pubdate, isbn, message, employee, 1);
				
				int flag = recommendImpl.insertOne(recommend);
				if (flag == 1) {
					globalMap.put(STATUS_CODE, SUCCESS);
				} else {
					globalMap.put(STATUS_CODE, INSERT_WR);
				}
			} else {
				globalMap.put("message", "该书已存在！");
			}
			
			
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	@POST
	@Path("updateARecommend/{idadmin}/{id}")
	@Produces("application/json; charset=utf-8")
	public String updateARecommend(@PathParam("idadmin") String idadmin, @FormParam("state") int state, 
			@Context HttpServletRequest request, @PathParam("id") int id) {
		Map<String, Object> globalMap = new HashMap<String, Object>();
		
		if (Login.isLoginAdmin(idadmin, request)) {
			
			List<Recommend> recommends = recommendImpl.selectById(id);
			if (recommends.size() > 0) {
				Recommend recommend = recommends.get(0);
				recommend.setState(state);
				int flag = recommendImpl.updateOne(recommend);
				if (flag == 0) {
					globalMap.put(STATUS_CODE, SELECT_WR);
				} else {
					globalMap.put(STATUS_CODE, SUCCESS);
				}
			} else if (recommends.size() == 0) {
				globalMap.put(STATUS_CODE, SELECT_EMPTY);
			} else {
				globalMap.put(STATUS_CODE, SELECT_WR);
			}
			
		} else {
			globalMap.put(STATUS_CODE, NOT_LOGGED_IN);
		}
		
		return toJsonUtils.map2Json(globalMap);
	}
	
	
	
}
